﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.DataAccess;
using University.Workshop.Model;

namespace University.Workshop.Business
{
    public class ManagerAllotment
    {
        public static List<Model.Allotment> SearchActiveAllotments()
        {
            return DataAccess.DataAccessAllotment.SearchActiveAllotments();
        }

        public static ICollection<Model.Allotment> GetAllotmentNotAssignedInSquare(Model.Square square)
        {
            return DataAccess.DataAccessSquare.SearchSquareNotHaveKardexInSquare(square);
        }

        public static Model.Allotment GetAllotmentNotAssigned(Model.Allotment allotment)
        {
            List<Model.Allotment> allotments = DataAccess.DataAccessAllotment.SearchAllotmentsNotAssigned(allotment);
            if (allotments.Count == 0)
                return null;
            else
                return allotments.First();
        }

        public static ICollection<Model.Allotment> GetAllotmentNotAssignedInSquareWithAssignedToKardex(Model.Square square, Model.Kardex kardex)
        {
            Model.Kardex k = ManagerKardex.GetKardexByKardexId(kardex);
            if (kardex.AllotmentModel != null)
                return DataAccess.DataAccessSquare.SearchSquareNotHaveKardexInSquareExceptKardex(square, kardex);
            else
                return DataAccess.DataAccessSquare.SearchSquareNotHaveKardexInSquare(square);

        }

        public static void NewAllotmentInSquare(ICollection<Model.Allotment> newAllotments, Model.Square newSquare)
        {
            ICollection<Model.Allotment> newAllots = new List<Model.Allotment>();
            foreach (Model.Allotment a in newAllotments)
            {
                a.SquareId = newSquare.SquareId;
                a.Enable = true;
                newAllots.Add(a);
            }
            if (newAllots.Count != 0)
            {
                foreach (Model.Allotment a1 in newAllots)
                {
                    ManagerAllotment.RegisterNewAllotWithSquare(a1);
                }
            }
        }

        public static void RegisterNewAllotWithSquare(Model.Allotment allotment)
        {
            DataAccessAllotment.InsertNewAllotment(allotment);
        }

        public static ICollection<Model.Allotment> AddNewAllotmentToList(string AllotmentName, ICollection<Model.Allotment> list)
        {
            if (string.IsNullOrWhiteSpace(AllotmentName))
                throw new Exception("El lote que desea ingresar esta vacio debe ingresar por lo menos un caracter.");
            if (list.Where(a => a.AllotmentName == AllotmentName).Count()!=0)
                throw new Exception("El Lote que desea ingresar ya fue ingresado en la lista.");
            list.Add(new Model.Allotment() { AllotmentName = AllotmentName });
            return list;
        }

        public static void VerifyAllotmentsIsCorrect(ICollection<Model.Allotment> allots)
        {

        }

        
        public static ICollection<Model.Allotment> GetAllotmentsBySquare(Model.Square square)
        {
            return DataAccessAllotment.SearchAllotmentBySquareIdActive(square);
        }

        public static Model.Allotment GetAllotmentNotAssignedWithoutKardex(Model.Kardex mp)
        {

            List<Model.Allotment> allotments = DataAccess.DataAccessAllotment.SearchAllotmentsNotAssignedToKardex(mp);
            if (allotments.Count == 0)
                return null;
            else
                return allotments.First();
        }

        public static void DeleteKardexFromAllotment(Model.Kardex KardexForDisable)
        {
            DataAccessAllotment.DeleteKardexFromAllotment(KardexForDisable.AllotmentModel);
        }

        internal static ICollection<Model.Allotment> GetAllotmentsOcupedInSquare(Model.Square square)
        {
            return DataAccessAllotment.SearchAllotmentsOcupedInSquare(square);
        }

        public static void CanDeleteAllotmentInSquare(Model.Allotment allotment, Model.Square square)
        {
            if(DataAccessAllotment.SearchAllotmentInSquareOcuped(allotment, square).Count!=0)
                throw  new Exception("El lote no puede ser inhabilitado por que actualemnte se encuentra ocupado");
        }

        public static void UpdateAllotmentsInSquare(ICollection<Model.Allotment> modifyAllotments, Model.Square modifySquare)
        {
            ICollection<Model.Allotment> allots = DataAccessAllotment.SearchAllotmentBySquareId(modifySquare);
            foreach (var allotment in allots)
            {
                DataAccessAllotment.ChangeStateToDisableAllotment(allotment);
            }
            foreach (var modifyAllotment in modifyAllotments)
            {
                if(DataAccessAllotment.SearchAllotmentByNameAndSquare(modifyAllotment,modifySquare).Count!=0)
                {
                    DataAccessAllotment.ChangeStateToEnableAllotment(modifyAllotment,modifySquare);
                }
                else
                {
                    DataAccessAllotment.InsertNewAllotment(new Allotment{SquareId = modifySquare.SquareId, Enable = true, AllotmentName = modifyAllotment.AllotmentName});
                }
            }
        }
    }
}
